package cn.csg.iotplatform.module.system.service.userdept;

import cn.csg.iotplatform.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csg.iotplatform.module.system.dal.dataobject.dept.DeptDO;
import cn.csg.iotplatform.module.system.dal.mysql.dept.DeptMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.csg.iotplatform.module.system.controller.admin.userdept.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.userdept.UserDeptDO;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;

import cn.csg.iotplatform.module.system.dal.mysql.userdept.UserDeptMapper;

import static cn.csg.iotplatform.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户设备单位授权 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
@Slf4j
public class UserDeptServiceImpl implements UserDeptService {

    @Resource
    private UserDeptMapper userDeptMapper;
    @Resource
    private DeptMapper deptMapper;

    @Override
    @Transactional
    public void createUserDept(UserDeptSaveReqVO createReqVO) {
        userDeptMapper.delete(UserDeptDO::getUserId,createReqVO.getUserId());
        List<UserDeptDO> list = new ArrayList<>();
        for(Long deptId : createReqVO.getDeptIds()){
            UserDeptDO userDept = BeanUtils.toBean(createReqVO, UserDeptDO.class);
            userDept.setDeptId(deptId);
            list.add(userDept);
        }
        userDeptMapper.insertBatch(list,1000);
    }

    @Override
    public UserDeptAuthRespVO getAuthDeptIds(Long userId) {
        List<UserDeptDO> list = userDeptMapper.getAuthDeptIds(userId);
        return UserDeptAuthRespVO.builder().autoAuth(list.isEmpty()?0:list.get(0).getAutoAuth()).authDeptIds(list.stream().map(UserDeptDO::getDeptId).collect(Collectors.toList())).build();
    }

    @Override
    public void upadteAutoAuthChildDepts(boolean onlyDelete,List<Long> deptIds) {
        if(onlyDelete){
            LambdaUpdateWrapper<UserDeptDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(UserDeptDO::getDeptId,deptIds);
            userDeptMapper.delete(updateWrapper);
        }else{
            List<UserDeptDO> addList = new ArrayList<>();
            List<Long> deleteList = new ArrayList<>();
            for(Long deptId : deptIds){
                DeptDO deptDO = deptMapper.selectDeptById(deptId);
                if(deptDO.getProjectAttr()){
                    List<Long> todoAuthUserIds = userDeptMapper.selectAutoAuthUserIds(deptId);
                    for(Long userId : todoAuthUserIds){
                        LambdaQueryWrapper<UserDeptDO> queryWrapper = new LambdaQueryWrapper<>();
                        queryWrapper.eq(UserDeptDO::getUserId,userId);
                        queryWrapper.eq(UserDeptDO::getDeptId,deptId);
                        queryWrapper.select(UserDeptDO::getUserId,UserDeptDO::getDeptId);
                        List<UserDeptDO> list = userDeptMapper.selectList(queryWrapper);
                        if(list.isEmpty()){
                            addList.add(UserDeptDO.builder().userId(userId).deptId(deptId).autoAuth(1).build());
                        }
                    }
                }else{
                    deleteList.add(deptId);
                }
            }
            if(deleteList.size() > 0){
                LambdaUpdateWrapper<UserDeptDO> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.in(UserDeptDO::getDeptId,deleteList);
                userDeptMapper.delete(updateWrapper);
            }
            if(addList.size() > 0 ){
                userDeptMapper.insertBatch(addList,1000);
            }
        }
    }

}